﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        int n = 10;
        int leng = 100;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Pen pen = new Pen(Color.Blue);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics =panel1.CreateGraphics();
            else
            {
                graphics.Clear(Color.White);
            }
            drawCayleyTree(n, this.Width/3, this.Height*3/4, leng, -Math.PI / 2);
        }

        void drawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            if (n == 0) return;
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);
            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(pen, (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            textBox1.Text = trackBar1.Value.ToString();
            n = trackBar1.Value;
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            textBox2.Text = trackBar2.Value.ToString();
            leng = trackBar2.Value;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            if (trackBar3.Value == 10)
                textBox3.Text = "1";
            else
                textBox3.Text = "0."+trackBar3.Value.ToString();
            per1 = (trackBar3.Value)*0.1;
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            if (trackBar4.Value == 10)
                textBox4.Text = "1";
            else
                textBox4.Text = "0." + trackBar4.Value.ToString();
            per2 = (trackBar4.Value) * 0.1;
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            textBox5.Text = ((double)trackBar5.Value*Math.PI/180).ToString();
            th1 = (trackBar5.Value) *Math.PI/180;
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            textBox6.Text = ((double)trackBar6.Value * Math.PI / 180).ToString();
            th2 = (trackBar6.Value) * Math.PI / 180;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            string color = comboBox1.Text;
            switch (color)
            {
                case "红色":
                    pen = new Pen(Color.Red);
                    break;
                case "蓝色":
                    pen = new Pen(Color.Blue);
                    break;
                case "黑色":
                    pen = new Pen(Color.Black);
                    break;
                case "黄色":
                    pen = new Pen(Color.Yellow);
                    break;
                case "绿色":
                    pen = new Pen(Color.Green);
                    break;
            }
        }
    }
}
